/*
 * Copyright 2012 JooKar BV (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.android.simpleclock.views;

import java.util.Date;

import nl.jookar.android.simpleclock.IClockView;
import nl.jookar.android.simpleclock.R;
import nl.jookar.android.simpleclock.SimpleClockApp;
import android.content.Context;
import android.graphics.Color;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * @author Joost A. Bloemsma
 *
 */
public class AnimatedDigitView extends RelativeLayout implements IClockView {
	private static final String DEBUG_TAG = AnimatedDigitView.class.getName();
	
	private SingleAnimatedDigit digit0;
	private SingleAnimatedDigit digit1;
	private SingleAnimatedDigit digit2;
	private SingleAnimatedDigit digit3;
	private SingleAnimatedDigit digit4;
	private SingleAnimatedDigit digit5;
	private SingleAnimatedDigit digit6;
	private SingleAnimatedDigit digit7;
	
	private String format;
	
	
	/**
	 * Constructs a new AnimatedDigitView instance
	 * @param context
	 */
	public AnimatedDigitView(Context context) {
		super(context);
		init();
	}

	/**
	 * Constructs a new AnimatedDigitView instance
	 * @param context
	 * @param attrs
	 */
	public AnimatedDigitView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		create();
		getControls();
	}
	
	private void create() {
		LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.animateddigitsview, this, true);
	}
	
	private void getControls() {
		digit0 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit0);
		digit1 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit1);
		digit2 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit2);
		digit3 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit3);
		digit4 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit4);
		digit5 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit5);
		digit6 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit6);
		digit7 = (SingleAnimatedDigit)findViewById(R.id.singleAnimatedDigit7);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setTime(Date date) {
		String time = (String)DateFormat.format(this.format, date);
		char[] ch = time.toCharArray();
		
		digit7.setChar(ch[7]);
		digit6.setChar(ch[6]);
		digit5.setChar(ch[5]);
		digit4.setChar(ch[4]);
		digit3.setChar(ch[3]);
		digit2.setChar(ch[2]);
		digit1.setChar(ch[1]);
		digit0.setChar(ch[0]);
//		char ch = time.charAt(time.length() - 1);
//		digit0.setChar(ch);
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void set24H(boolean fmt24) {
		if (fmt24) {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_24h);
		} else {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_12h);
		}
	}
	
	
}
